package com.jie.client.service.ui.view;

import com.jie.client.core.ClientApplication;
import com.jie.client.core.handler.ClientWriteHandler;
import com.jie.client.service.entity.model.Group;
import com.jie.client.service.ui.constant.ViewMapConstant;
import com.jie.client.service.ui.model.SetNode;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 群聊视图
 * @author XieZhiJie
 * @date 2022/04/29 11:32
 */
public class GroupChatView {

    /**
     * 正在聊天的群信息
     */
    private final Group group;

    /**
     * 记录接收到的消息
     */
    private final TextArea groupChatMessage = new TextArea();

    /**
     * 记录当前用户要发送的信息
     */
    private final TextArea groupChatSendMessage = new TextArea();

    public GroupChatView(Group group) {
        this.group = group;
    }

    public void createGroupChatView() {
        Stage stage = new Stage();

        VBox gcBox = new VBox();
        // 顶部的群聊信息
        Text gcText = new Text();
        Text gcSendMsgText = new Text("向该群发送消息");
        Button groupMemberButton = new Button("群聊成员");
        Button chatRecordButton = new Button("聊天记录");
        Button gcSendMsgButton = new Button("发送消息");

        ClientApplication.VIEW_MAP.put("groupChatMessage" + this.group.getGroupId(), this.groupChatMessage);

        // 放聊天记录和共享画板功能的Box
        HBox functionBox = new HBox();

        // 设置HBox内控件的间距
        functionBox.setSpacing(20);
        HBox.setMargin(gcSendMsgText, new Insets(10 ,0, 0, 0));
        HBox.setMargin(groupMemberButton, new Insets(0, 0, 0, 563));

        // 把pcSendMsgText文字和聊天记录功能放入functionBox中
        functionBox.getChildren().add(gcSendMsgText);
        functionBox.getChildren().add(groupMemberButton);
        functionBox.getChildren().add(chatRecordButton);

        // 设定顶部信息
        gcText.setText("\t\t\t\t\t\t\t\t\t\t\t" + this.group.getGroupName() + "(" + this.group.getGroupId() +  ")");

        // 消息窗口不可输入
        this.groupChatMessage.setEditable(false);

        // 消息窗口的颜色
        this.groupChatMessage.setStyle("-fx-text-fill: mediumvioletred");

        // 绑定事件
        gcSendMsgButton.setOnAction(event -> onGroupChatSendMessageAction());
        groupMemberButton.setOnAction(event -> onGroupMemberAction());
        chatRecordButton.setOnAction(event -> onChatRecordAction());

        // 添加控件
        gcBox.getChildren().add(gcText);
        gcBox.getChildren().add(this.groupChatMessage);
        gcBox.getChildren().add(functionBox);
        gcBox.getChildren().add(this.groupChatSendMessage);
        gcBox.getChildren().add(gcSendMsgButton);

        // 设置控件位置
        gcBox.setPadding(new Insets(10, 15, 10, 15));
        gcBox.setSpacing(20);

        // 设置聊天信息框大小
        this.groupChatMessage.setMinHeight(450);

        // 设置发送消息按钮位置
        VBox.setMargin(gcSendMsgButton, new Insets(0 ,0 ,0, 790));

        // 设置场景
        Scene gcScene = new Scene(gcBox, 900, 700);
        stage.setScene(gcScene);
        stage.setTitle("群聊窗口");
        stage.show();

        // 关掉窗口的时候移除该群聊id
        stage.setOnCloseRequest(event -> {
            ((SetNode) ClientApplication.VIEW_MAP.get(ViewMapConstant.GROUP_CHAT_SET)).set.remove(group.getGroupId());
            ClientApplication.VIEW_MAP.remove("groupChatMessage" + this.group.getGroupId());
        });
    }

    /**
     * 发送信息按钮
     */
    private void onGroupChatSendMessageAction() {
        if (this.groupChatSendMessage.getText() == null || "".equals(this.groupChatSendMessage.getText())) {
            return;
        }
        // 封装请求
        Map<String, Object> map = new HashMap<>(4);
        map.put("choice", "chatToGroup");
        map.put("groupId", this.group.getGroupId());
        map.put("sendMessage", groupChatSendMessage.getText().trim());
        ClientWriteHandler.sendRequest(map);
        // 更新自己的私聊窗口
        // 是用本地时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        synchronized (this.groupChatMessage) {
            this.groupChatMessage.appendText(
                    "\n" + ClientApplication.USER.getUsername() + "(" + ClientApplication.USER.getUserAccount() + ") " + simpleDateFormat.format(new Date())
                            + " (我)"
                            + "\n" + groupChatSendMessage.getText().trim()
            );
        }
        this.groupChatSendMessage.clear();
    }

    /**
     * 点击获取群聊用户列表
     */
    public void onGroupMemberAction() {
        Map<String, Object> map = new HashMap<>(2);
        map.put("choice", "getGroupUserList");
        map.put("groupId", this.group.getGroupId());
        ClientWriteHandler.sendRequest(map);
    }

    /**
     * 点击聊天记录按钮
     */
    public void onChatRecordAction() {
        Map<String, Object> map = new HashMap<>(4);
        map.put("choice", "chatToGroupRecords");
        map.put("targetGroupId", this.group.getGroupId());
        // 创建出窗口后再发送请求
        new ChatRecordView(this.group).createChatRecordView();
        ClientWriteHandler.sendRequest(map);
    }

}
